import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConsoleLogger, VersioningType } from '@nestjs/common';
import compression from 'compression';
import cookieParser from 'cookie-parser';
import session from 'express-session';
import { ConfigService } from '@nestjs/config';


async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: new ConsoleLogger({
      logLevels: ['log', 'error', 'warn', 'debug', 'verbose'],
      prefix: 'Leslie Nest App',
      json: false,
      colors: true,
      compact: true,
    })
  });
  const configService = app.get(ConfigService);
  app.enableCors({
    origin: true,
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    credentials: true,
  });
  app.enableVersioning({
    type: VersioningType.HEADER,
    header: 'Custom-Header',
  });
  // register express-session middleware
  app.use(session({
    secret: 'leslie-secret',
    resave: false, // whether to save session even if it's not modified
    saveUninitialized: false, // whether to save uninitialized session
    cookie: {
      httpOnly: true, // whether the cookie is only accessible via HTTP(S) requests
      secure: process.env.NODE_ENV === 'production', // whether the cookie is only sent over HTTPS
      maxAge: 1000 * 60 * 60, // session expiration time in milliseconds
      sameSite: 'lax'
    },
    store: new session.MemoryStore(),
  }))
  // Starts listening for shutdown hooks
  app.enableShutdownHooks()
  app.use(compression());
  app.use(cookieParser());
  const appPort = configService.get<number>('PORT',1983)

  await app.listen(appPort, () => {
    console.info(
      `App is running on port: http://localhost:${appPort}`,
    );
  });
}
bootstrap();
